var tableIns;  // 在子窗口中通过 parent.tableIns 可以访问
layui.use(['layer', 'table','jquery','form'], function () {
    var layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.$,
        form = layui.form,
        table = layui.table;

    //列表
    tableIns = table.render({
        elem: '#appList',
        url: 'http://localhost:8080/api/Apps/',
        headers: {'auth': sessionStorage.getItem('token')},
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limit: 8,
        limits: [5, 8,10, 15, 20],
        id: "appList",
        cols: [[
            { type: "checkbox", fixed: "left", width: 50 },
            { type: 'numbers', title: '序号', width: '80' },
            { field: 'Name', title: '名称', width: 150 },
            { field: 'CategoryName', title: '类别', width: 150 },
            { field: 'DevUser', title: '作者', width: 150 },
            { title: '操作', width: 190, templet: '#opt', fixed: "right", align: "center" }
        ]]
    });


    //搜索
    $(".search_btn").on("click", function () {
        if ($(".searchVal").val() != '') {
            table.reload("appList", {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val()  //搜索的关键字
                }
            })
        } else {
            table.reload("appList", {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: null
            });
        }
    });

    //添加
    $(".btnAdd").click(function () {
        var index = layui.layer.open({
            title: "添加APP",
            type: 2,
            content: "add.html",
            area: ['60%', '450px'],
            success: function (layero, index) {
            }
        })
    })


    //批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('appList'),
            data = checkStatus.data,
            appsId = [];
        if (data.length > 0) {
            for (var i in data) {
                appsId.push(data[i].Id);
            }
            layer.confirm('确定删除选中的APP吗？', { icon: 3, title: '提示信息' }, function (index) {
                $.ajax({
                    url: 'http://localhost:8080/api/apps/',
                    type: 'delete',
                    contentType: 'application/json',
                    data: JSON.stringify(appsId),
                    success: function (result) {
                        layer.msg('删除成功！');
                        tableIns.reload();
                        console.log(result);
                    },
                    error: function () {
                        layer.msg('删除失败！')
                    }
                })
                layer.close(index);
            })
        } else {
            layer.msg("请选择需要删除的角色");
        }
    })

    //列表操作
    table.on('tool(appList)', function (obj) {
        var layEvent = obj.event, //事件名称
            data = obj.data;  // 当前行数据
        if (layEvent === 'edit') { //编辑
            //查询APP
            $.ajax({
                url: 'http://localhost:8080/api/apps/' + data.Id,
                type:'get',
                success: function (result) {
                    var index = layui.layer.open({
                        title: "修改",
                        type: 2,
                        content: "edit.html",
                        area: ['50%', '450px'],
                        success: function (layero, index) {
                            var iframeWin = window[layero.find('iframe')[0]['name']];
                          
                            iframeWin.appInfo = result;
                        }
                    })
                }
            })
            
        } else if (layEvent === 'del') { //删除
            layer.confirm('确定删除此APP？', { icon: 3, title: '提示信息' }, function (index) {
                $.ajax({
                    url: 'http://localhost:8080/api/apps/' + data.Id,
                    type: 'delete',
                    success: function (result) {
                        layer.msg(result);
                        tableIns.reload();
                    }
                })

            });
        } 
    });

   

})